<?php
class TF_Form extends Zend_Form
{
	private $_customDecorators = array();
    private $_messages = array('isEmpty' => 'Tato položka je povinná.' , 'notInt' => 'Hodnota "%value%" není platná pro tuto položku. ' ,
        'notBetween' => 'Hodnota "%value%" není v povoleném rozsahu (%min% - %max%). ' , 'notSame' => 'Tato položka neodpovídá.');
    private $_translate = null;
    public $formDecorators = array('formElements' , array('htmlTag' , array('tag' => 'div' , 'class' => 'group')) ,
        array('Form' , array('class' => 'nice'),));
   	public $oneLineFormDecorators = array('formElements' , array('htmlTag' , array('tag' => 'div' , 'class' => 'one-line-group')),array('FormErrors',array('placement'=>'append')) ,
        array('Form' , array('class' => 'nice')));
    public $elementDecorators = array('viewHelper' , array('Errors' , array('placement' => 'append')) ,
        array('Description' , array('placement' => 'append' , 'tag' => 'div' , 'class' => 'description', 'escape'=>false)) ,
        array('Label' , array('placement' => 'preppend')) , array('htmlTag' , array('tag' => 'div' , 'class' => 'element')));
    public $oneLineDecorators = array(
    	'viewHelper', 
    	
    );
    public $oneLineButtonDecorators = array(
    	'viewHelper', 
    	array('htmlTag',array('tag'=>'span','class'=>'button')),
    	
    );
    public $checkboxDecorators = array(
    	'viewHelper' , 
    	array(
    		'Errors' , 
    		array(
    			'placement' => 'append'
    		)
    	),
        array(
        	'Description' , 
        	array(
        		'placement' => 'append' ,
        		'tag' => 'div' , 
        		'class' => 'description',
        		'escape'=>false
        	)
        ),
        array(
        	'Label', 
        	array(
        		'placement' => 'preppend'
        	)
        ), 
        array(
        	'htmlTag',
        	array(
        		'tag' => 'div',
        		'class' => 'element checkbox'
        	)
        )
	);
    public $multiDecorators = array(array('viewHelper',array('class'=>'multiElement')) ,
        array(array('cleaner' => 'htmlTag') , array('tag' => 'div' , 'class' => 'clear', 'placement' => 'append')) ,
    	array(array('elementGroup' => 'htmlTag') , array('tag' => 'div' , 'class' => 'elementGroup')) ,
        array('Description' , array('placement' => 'append' , 'tag' => 'div' , 'class' => 'description', 'escape'=>false)) ,
        array('Errors' , array('placement' => 'append')) , array('Label' , array('placement' => 'preppend')) ,
        array(array('element' => 'htmlTag') , array('tag' => 'div' , 'class' => 'element')));
    public $buttonDecorators = array('viewHelper' , array('htmlTag' , array('tag' => 'div' , 'class' => 'element button')));
    public $groupDecorators = array('formElements' , 'Fieldset' , array('htmlTag' , array('tag' => 'div' , 'class' => 'group')));
    /**
     *
     *@param mixed $options
     *@return void
     */
    public function __construct ($options = null)
    {
        parent::__construct($options);
        $this->setDefaultDecorators();
        $this->addElementPrefixPath('TF_Form_Element_', 'TF/Form/Element/');
        $this->init();
    }
    /**
     * setDefaultDecorators
     */
    public function setDefaultDecorators ()
    {
        $this->setDecorators(array());
        $this->setDecorators($this->formDecorators);
    } /* of setDefaultDecorators -------------------------------------*/
    
    public function setOneLineDecorators()
    {
    	$this->setDecorators(array());
        $this->setDecorators($this->oneLineFormDecorators);
        foreach ($this->getElements() as $el){
        	switch ($el->helper) {
                case 'formSubmit':
                case 'formReset':
                    $el->setDecorators($this->oneLineButtonDecorators);
                    break;
                default:
                    $el->setDecorators($this->oneLineDecorators);
                    break;
            }
        }
    }
    /**
     * setElementDefaultDecorators
     */
    public function setElementDefaultDecorators ()
    {
        foreach ($this->getSubForms() as $sf) {
            $sf->setElementDefaultDecorators();
        }
        foreach ($this->getElements() as $el) {
        	if(in_array($el->getName(),$this->getCustomDecorators()) ){
        		continue;
        	}
            //echo '|'.$el->getName().' is '.$el->helper;
            switch ($el->helper) {
                case 'formSubmit':
                case 'formReset':
                    $el->setDecorators($this->buttonDecorators);
                    break;
                case 'formMultiCheckbox':
                case 'formMultiSelect':
                case 'formRadio':
                    $el->setDecorators($this->multiDecorators);
                    break;
                case 'formCheckbox':
                	$el->setDecorators($this->checkboxDecorators);
                	break;
                default:
                    $el->setDecorators($this->elementDecorators);
                    break;
            }
        }
        foreach ($this->getDisplayGroups() as $group) {
            $group->setDecorators($this->groupDecorators);
        }
    } /* of setElementDefaultDecorators -------------------------------------*/
    /**
     * Load the default decorators
     */
    public function loadDefaultDecorators ()
    {
        if ($this->loadDefaultDecoratorsIsDisabled()) {
            return;
        }
        $decorators = $this->getDecorators();
        if (empty($decorators)) {
            $this->setDecorators($this->formDecorators);
        }
    }
    /**
     * setMessages
     */
    public function setMessages (array $messages)
    {
        $this->_messages = $messages;
    } /* of setMessages -------------------------------------*/
    
    public function setCustomDecorators($elements)
    {
    	if($elements == '*'){
    		foreach($this->getElements() as $element){
    			$this->_customDecorators[] = $element->getName();
    		}
    	} else {
    		$this->_customDecorators = $elements;
    	}
    	
    	return $this;	
    }
    
    public function getCustomDecorators()
    {
    	return $this->_customDecorators;
    }
    
    public function render(Zend_View_Interface $view = null)
    {
    	$this->setElementDefaultDecorators();
    	return parent::render($view);
    }
}